<?php

class Data extends Controller {
    function Data() {
        parent::Controller();
        $this->load->model('model_data');
        //verificare sesiune
        if (!$this->usersession->userLoggedIn()) {
            $aResponse = array("error" => true, "description" => "Sesiunea a expirat", "type" => "nosession");
            die(json_encode($aResponse));
        }
    }

    /**
     * functie de adaugare generala din meniu Operatii
     */
    function addObject() {
        $data = trim($this->input->post('data'));
        $table = $this->input->post('table');
        $data_parent = trim($this->input->post('data_parent'));
        if (trim($data) == '') {
            $aResponse = array("error" => true, "description" => "Element nul");
            echo json_encode($aResponse);
            return;
        }
        $result = $this->model_data->addObject($data, $table, $data_parent);
        if ($result) {
            $aResponse = array("error" => false, "description" => "Datele au fost salvate", "type" => "data");
        }
        else {
            $aResponse = array("error" => true, "description" => "Exista duplicat", "type" => "data");
        }
        echo json_encode($aResponse);
    }

    function bringObject() {
        $result = $this->model_data->bringObject($_POST);
        echo "{'error':false, 'description':'Datele au fost aduse', 'type':'silent' ,'rows':" . json_encode($result) . "}";
    }

    function bringCompany() {
        $result = $this->model_data->bringCompany($_POST);
        echo '{rows:' . json_encode($result) . '}';
    }

    /**
     * Functie care aduce domeniul - concatenat cu subdomeniul. In plus se aduce si domeniul simplu ( fara concatenare de subdomeniu ) pentru cazul in care se doreste doar completarea domeniului
     */
    function bringSubdomain() {
        $result = $this->model_data->bringSubdomain($_POST);
        echo '{rows:' . json_encode($result) . '}';
    }

    /*function bringMultiObject() {
       $result = $this->model_data->bringMultiObject($_POST);
       echo '{rows:' . json_encode($result) . '}';
   } */

    /*
    function bringTop() {
        $table = 'persons';
        $s_limit = $this->input->post("start");
        $e_limit = $this->input->post("limit");
        $dir = $this->input->post("dir");
        $sort = $this->input->post("sort");
        echo json_encode($this->model_data->bringTop($table, $s_limit, $e_limit, $sort, $dir, $_POST));
    }  */


}
